import cloneDeep from 'lodash/cloneDeep';
import isEqual from 'lodash/isEqual';
import pick from 'lodash/pick';
import omit from 'lodash/omit';
export const ObjectUtil = {
	/**
	 * @author 戴俊明 <idaijunming@163.com>
	 * @description 0 和 '' 为 true, null 和 undefined 为 false
	 * @date 2020/6/6 0:17
	 **/
	notNull: o => {
		// eslint-disable-next-line eqeqeq
		return o != null;
	},
	has: (o, key) => {
		return Object.prototype.hasOwnProperty.call(o, key);
	},
	equals: (a, b, ignoreKeys = []) => {
		return isEqual(omit(a, ignoreKeys), omit(b, ignoreKeys));
	},
	clone: o => cloneDeep(o),
	pick: (o, keys = []) => pick(o, keys),
	omit: (o, ignoreKeys = []) => omit(o, ignoreKeys),
	assign: (oldVal, newVal, ignoreKeys = []) => {
		let clone = cloneDeep(oldVal);
		clone = Object.assign(clone, newVal);
		for (const ignoreKey of ignoreKeys) {
			delete clone[ignoreKey];
		}
		return clone;
	},
	toJSON: o => JSON.stringify(o),
	fromJSON: json => {
		try {
			return JSON.parse(json);
		} catch (e) {
			return null;
		}
	}
};
